package com.hundsun.yr.universal.p2p.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.adapter.ViewCreatorHelper.UpdateItem;
import com.hundsun.yr.universal.library.base.BaseAppCompatActivity;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.view.banner.BannerAutoLoopLayout;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.PermissionsUtils;

import java.util.List;

/**
 * 引导页
 *
 * @author tonglj
 */
@SuppressLint("ClickableViewAccessibility")
public class GuideActivity extends BaseAppCompatActivity implements OnClickListener {
    private String[] permissions = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };
    /**
     * binner页面
     */
    private BannerAutoLoopLayout<Integer> mBannerAutoLoopLayout;
    /**
     * banner图片数据
     */
    private List<Integer> banners;
    /**
     * 跳过UI
     */
    private TextView tv_skip;

    private HSSharedPreferences sharedPreferences;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.skip:
//                // SDK初始化，第三方程序启动时，都要进行SDK初始化工作
//                PackageManager pkgManager = getPackageManager();
//                // 读写 sd card 权限非常重要, android6.0默认禁止的, 建议初始化之前就弹窗让用户赋予该权限
//                boolean sdCardWritePermission =
//                        pkgManager.checkPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, getPackageName()) == PackageManager.PERMISSION_GRANTED;
//
//                // read phone state用于获取 imei 设备信息
//                boolean phoneSatePermission =
//                        pkgManager.checkPermission(Manifest.permission.READ_PHONE_STATE, getPackageName()) == PackageManager.PERMISSION_GRANTED;

                if (Build.VERSION.SDK_INT >= 23) {
                    PermissionsUtils.getInstance().chekPermissions(GuideActivity.this, permissions, permissionsResult);
                } else {
                    sharedPreferences.putBoolean(Config.IF_GOGUIDE, true);
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("ISFIRSTOPEN", true);
                    readyGoThenKill(MainActivity.class, bundle);
                }
                break;
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_guide;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void initViewsAndEvents() {
        findViews();
        sharedPreferences = new HSSharedPreferences(mContext, Config.SPPath.file_userLoginedInfo);
        setSystemBarTintDrawable(getResources().getDrawable(R.color.gray));
        banners = CommTool.getResIntList(mContext, R.array.duide_pages);
        tv_skip.setOnClickListener(this);
        mBannerAutoLoopLayout.setAdapter(R.layout.item_pager_image, new UpdateItem<Integer>() {
            @Override
            public void refreshItem(View view, int position, Integer resid) {
                ImageView iv_img = (ImageView) view.findViewById(R.id.image);
//                iv_img.setImageResource(resid);
                Glide.with(GuideActivity.this).load(resid).into(iv_img);
            }
        });
        sharedPreferences = new HSSharedPreferences(mContext, Config.SPPath.file_configInfo);
        mBannerAutoLoopLayout.updata(banners);

        if (mBannerAutoLoopLayout.getLoopViewPager() != null) {

            mBannerAutoLoopLayout.getLoopViewPager().addOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(final int position) {
                    if (position + 1 == banners.size()) {//滑动到最后一页
                        tv_skip.setVisibility(View.VISIBLE);
                    } else {
                        tv_skip.setVisibility(View.GONE);
                    }
//					mBannerAutoLoopLayout.getLoopViewPager().setOnTouchListener(new OnTouchListener() {
//
//						float start;
//					    long starttime;
//
//						@Override
//						public boolean onTouch(View v, MotionEvent event) {
//
//							switch (event.getAction()) {
//							case MotionEvent.ACTION_DOWN:
//								start = event.getX();
//								starttime = event.getDownTime();
//								break;
//
//							case MotionEvent.ACTION_UP:
//								float end = event.getX();
//								long endtime = event.getEventTime();
//								float velocity = (start - end)
//										/ Float.parseFloat((String.valueOf(endtime - starttime)));
//								if (velocity > 3.00 && position == 3) {
//									readyGoThenKill(MainActivity.class);
//								}
//								break;
//							}
//							return false;
//						}
//					});
//					;
                }

                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageScrollStateChanged(int arg0) {

                }
            });
        }

    }

    @SuppressWarnings("unchecked")
    private void findViews() {
        mBannerAutoLoopLayout = (BannerAutoLoopLayout<Integer>) findView(R.id.banner_layout);
        tv_skip = findView(R.id.skip);
    }

//    @Override
//    protected boolean isApplyKitKatTranslucency() {
//        return false;
//    }

    @SuppressWarnings("rawtypes")
    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    //创建监听权限的接口对象
    PermissionsUtils.IPermissionsResult permissionsResult = new PermissionsUtils.IPermissionsResult() {
        @Override
        public void passPermissons() {
//            Toast.makeText(GuideActivity.this, "权限通过，可以做其他事情!", Toast.LENGTH_SHORT).show();
            sharedPreferences.putBoolean(Config.IF_GOGUIDE, true);
            Bundle bundle = new Bundle();
            bundle.putBoolean("ISFIRSTOPEN", true);
            readyGoThenKill(MainActivity.class, bundle);
        }

        @Override
        public void forbitPermissons() {
            finish();
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //就多一个参数this
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);

    }


}
